package util.io;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStream;

import java.io.IOException;

import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;



/**
 * This class enclose some functions to process text data
 * It opens a buffer reader pointing to a file which path  is specified by the user
 * methods to close and read lines are provided.
 * @author Sergio Duarte
 *
 */
public class FileInput  {

	
	private BufferedReader reader;
	Scanner scanner ;
	private String filename;

	private boolean eof = false;

	 private static final int DEFAULT_BUFFER_SIZE = 1024 * 40;

	

	/**
	 * The constructor receive the path of the file that the use wants to read
	 */
	
	public FileInput(String fname) {
		filename = fname;

		try {
			File file = new File(fname);
			FileInputStream instr = new FileInputStream(filename);
			InputStreamReader instrR = new InputStreamReader(instr,"utf-8");
		
			 scanner = new Scanner(file);
			
		//	reader = new BufferedReader(instrR,DEFAULT_BUFFER_SIZE);
	
			
		
	
		//	  reader = new BufferedReader(new FileReader(fname));

		//	reader = new BufferedReader(new FileReader(filename));
		} catch (FileNotFoundException e) {
			error("Can't open file: " + filename);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/*
	 * 
	 * Receives reader  directly
	 * 
	 */
	public FileInput(BufferedReader reader) {
		
		this.reader= reader;
		
	}

	public FileInput(String fname, String encoding) {
		filename = fname;

		try {
			
			//FileInputStream instr = new FileInputStream(filename);
			//InputStreamReader instrR = new InputStreamReader(instr,encoding);
			//reader = new BufferedReader(instrR);
			  reader = new BufferedReader(new FileReader(fname));

			
			
		//	reader = new BufferedReader(new FileReader(filename));
		} catch (FileNotFoundException e) {
			error("Can't open file: " + filename);
		}// catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
		//	e.printStackTrace();
	//	}
	}
	
	/**
	 * 
	 * @return  string with the next line of the file
	 */

	public final synchronized String readString1() {

		String s = "";
		try {
			
			s = reader.readLine();
			if (s == null) {
				eof = true;

				//return dummy word
				return null;
			}
		} catch (IOException e) {
			error("readString failed for file: " + filename);
		}
		return s;
	}
	
	
	public final synchronized String readString() {

		String s = "";
	
			if(scanner.hasNext())
				s = scanner.nextLine();
			else{
				
				eof = true;
				return null;
			}
						//return dummy word
			
			
		
		return s;
	}
	
	
	/**
	 *  Close the file when finished
	 */
	public void close() {
		try {
			reader.close();
		} catch (IOException e) {
			error("Can't close file: " + filename);
		}
	}

	/**
	 *  Return true if the end of file has been reached.
	 */
	public boolean eof() {
		return eof;
	}
	
	
	
	public String getText() {
		StringBuffer buf = new StringBuffer("");

		if (this.reader != null) {

			String line = null;
			try {
				line = reader.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			while (line != null) {

				buf.append(line+ "\n");
				try {
					line = reader.readLine();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}

		return buf.toString();

	}

	private void error(String msg) {
		System.out.println(msg);
		System.out.println("Unable to continue executing program.");
		System.exit(0);
	}

	

}
